package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.MoveCloseSessionToHistorySessionTableBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.MoveCloseSessionToHistorySessionTableCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.model.YmtCssessionHistory;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
/**
*关闭会话迁移到历史表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MoveCloseSessionToHistorySessionTable{
	private static MoveCloseSessionToHistorySessionTableBean moveclosesessiontohistorysessiontableBean;
	private static MoveCloseSessionToHistorySessionTableCall moveclosesessiontohistorysessiontableCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("关闭会话迁移到历史表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		moveclosesessiontohistorysessiontableBean=new MoveCloseSessionToHistorySessionTableBean();
		moveclosesessiontohistorysessiontableCall=new MoveCloseSessionToHistorySessionTableCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(moveclosesessiontohistorysessiontableCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MoveCloseSessionToHistorySessionTable_001() {
		Logger.start(true,"关闭会话迁移到历史表成功,进入历史表");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			SellerCSIMCallService.OffLine(sellerId, null);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			
			moveclosesessiontohistorysessiontableBean.setAppId("auto");
			moveclosesessiontohistorysessiontableCall.setData(moveclosesessiontohistorysessiontableBean);
			moveclosesessiontohistorysessiontableCall.callService();			
			Logger.verifyEquals("ok", moveclosesessiontohistorysessiontableCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			List<YmtCssessionHistory> historycinfos = YmtSellerCSIMWapper.selectYmtCssessionHistory(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.debug(FormatUtil.beanToGSONString(historycinfos));
			Logger.verifyEquals(cinfos.get(0).getCloseType(),historycinfos.get(0).getCloseType(), "验证CloseType");
			Logger.verifyEquals(cinfos.get(0).getBeginTime(),historycinfos.get(0).getBeginTime(), "验证getBeginTime");
			Logger.verifyEquals(cinfos.get(0).getCSId(),historycinfos.get(0).getCSId(), "验证getCSId");
			Logger.verifyEquals(cinfos.get(0).getCSName(),historycinfos.get(0).getCSName(), "验证getCSName");
			Logger.verifyEquals(cinfos.get(0).getEndTime(),historycinfos.get(0).getEndTime(), "验证v");
			Logger.verifyEquals(cinfos.get(0).getFromPlatform(),historycinfos.get(0).getFromPlatform(), "验证getFromPlatform");
			Logger.verifyEquals(cinfos.get(0).getLastMessageTime(),historycinfos.get(0).getLastMessageTime(), "验证getLastMessageTime");
			Logger.verifyEquals(cinfos.get(0).getLaunchTime(),historycinfos.get(0).getLaunchTime(), "验证getLaunchTime");
			Logger.verifyEquals(cinfos.get(0).getMark(),historycinfos.get(0).getMark(), "验证getMark");
			Logger.verifyEquals(cinfos.get(0).getMasterCSId(),historycinfos.get(0).getMasterCSId(), "验证getMasterCSId");
			Logger.verifyEquals(cinfos.get(0).getOpenType(),historycinfos.get(0).getOpenType(), "验证getOpenType");
			Logger.verifyEquals(cinfos.get(0).getSessionNumber(),historycinfos.get(0).getSessionNumber(), "验证getSessionNumber");
			Logger.verifyEquals(cinfos.get(0).getStatus(),historycinfos.get(0).getStatus(), "验证getStatus");
			Logger.verifyEquals(cinfos.get(0).getUnReadCount(),historycinfos.get(0).getUnReadCount(), "验证getUnReadCount");
			Logger.verifyEquals(cinfos.get(0).getUserId(),historycinfos.get(0).getUserId(), "验证getUserId");
			Logger.verifyEquals(cinfos.get(0).getUserName(),historycinfos.get(0).getUserName(), "验证getUserName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MoveCloseSessionToHistorySessionTable_002() {
		Logger.start(true,"未关闭会话不进入历史表");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
//			SellerCSIMCallService.OffLine(sellerId, null);
//			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			
			moveclosesessiontohistorysessiontableBean.setAppId("auto");
			moveclosesessiontohistorysessiontableCall.setData(moveclosesessiontohistorysessiontableBean);
			moveclosesessiontohistorysessiontableCall.callService();			
			Logger.verifyEquals("ok", moveclosesessiontohistorysessiontableCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			List<YmtCssessionHistory> historycinfos = YmtSellerCSIMWapper.selectYmtCssessionHistory(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			Logger.verifyEquals(0, historycinfos.size(), "数据未进入历史表");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}